home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
stv.lha
/
STV
/
ISA
/
carolina
/
mdwsmltn.prj
< prev
next >
Wrap
Text File
|
1993-07-23
|
14KB
|
537 lines
"
******************************************************************************
Project : MeadowSimulation
Date : Jan 21, 1990
Time : 17:20:02
Classes :
MeadowAnimal Rabbit Grass Fox Meadow MeadowInhabitant
Methods :
******************************************************************************
"!
Object subclass: #Meadow
instanceVariableNames:
'foxes rabbits grasses date '
classVariableNames: ''
poolDictionaries: ''!
Object subclass: #MeadowInhabitant
instanceVariableNames:
'age location displayState '
classVariableNames: ''
poolDictionaries: ''!
MeadowInhabitant subclass: #MeadowAnimal
instanceVariableNames:
'lastAte lifeExpectancy birthRate foodRequirement lastBirth dailyRange '
classVariableNames: ''
poolDictionaries: ''!
MeadowAnimal subclass: #Rabbit
instanceVariableNames:
'lastEaten '
classVariableNames:
'RabbitIcon '
poolDictionaries: ''!
MeadowInhabitant subclass: #Grass
instanceVariableNames:
'lastEaten '
classVariableNames:
'GrassIcon '
poolDictionaries: ''!
MeadowAnimal subclass: #Fox
instanceVariableNames: ''
classVariableNames:
'FoxIcon '
poolDictionaries: ''!
!MeadowAnimal class methods !
example
" MeadowAnimal example "
"create instance of fox & rabbit"
"let them give birth and die"
| fox |
fox := Fox new displayState: true;
lastBirth: -2;
birthRate: 1;
lastAte: -2;
lifeExpectancy: 20;
foodRequirement: 1;
age: 0.
fox birth ifTrue:[fox lastBirth: 0].
fox death ifTrue:[fox displayState: false].
fox inspect.! !
!MeadowAnimal methods !
birth
"return true if display status is true
and (age-lastBirth> birthRate) else false"
^((self displayState) &
((self age)-(self lastBirth)
>(self birthRate)))!
birthRate: anInteger
"set the value for the animal's birth rate days/birth"
birthRate := anInteger!
death
"if (age> lifeExpectancy)
or age-lastAte>foodRequirement
or displayStatus = false
then return true, else false"
^((self age > self lifeExpectancy)
|((self age - self lastAte) > self foodRequirement)
| self displayState not)!
distanceTo: anObject
"return the distance from self to anObject"
|point distance guess|
point := self location - anObject location.
distance := (point dotProduct: point)sqrt.
^distance.!
lastAte
"return the day the animal last ate"
"default = 0"
lastAte isNil ifTrue:[self lastAte: 0].
^lastAte!
lastAte: anInteger
"set the age that animal last ate to anInteger"
lastAte := anInteger!
lastBirth
"return the age of animal at the last birth"
"default = 0"
lastBirth isNil ifTrue: [self lastBirth: 0].
^lastBirth!
lastBirth: anInteger
"set the age of animal at the last birth"
lastBirth := anInteger!
lifeExpectancy: anInteger
"set the value for the animal's life expectancy"
lifeExpectancy := anInteger!
locatePrey: aBag
"locate the nearest visible prey from a Bag"
"if distance to chosenPrey < dailyRange set prey displayState
to false to indicate being eaten"
|distance minDistance capturedPrey|
minDistance := 999. "default"
capturedPrey := nil. "default"
aBag do:[:eachPrey | (eachPrey displayState)
ifTrue: [distance := self distanceTo: eachPrey.
(distance < minDistance)
ifTrue: [minDistance := distance.
capturedPrey := eachPrey]]].
^capturedPrey!
moveToPrey: anObject
"if distance < dailyRange change location to
the location of the Prey and its displayState
to false set captor's IV lastAte to its age"
"if distance > dailyRange change location to
location+(difference in locations)*dailyRange/distance"
| distance |
(anObject isNil) ifTrue:[^nil].
distance := (self location dotProduct: (anObject location)) sqrt.
(distance <= self dailyRange )
ifTrue:[self location: anObject location.
anObject displayState: false.
anObject lastEaten: anObject age.
self lastAte: self age]
ifFalse:[self location:(self location
+(self location - anObject location)*
(self dailyRange ) // distance)]! !
!Rabbit class methods !
example
"display Icon at location"
" Rabbit example "
|rabbit|
rabbit := Rabbit new.
self icon displayAt: 120@100.
self inspect!
icon
"return the RabbitIcon"
RabbitIcon isNil ifTrue: [RabbitIcon := Form width: 16 height: 16
bitmap: #(63 252 63 248 31 242 95 246 79 230 103 206 119 222 55 220 55 220 176 13 176 5 167 241 140 153 204 153 239 123 239 123 )].
^RabbitIcon!
icon: aForm
"set the icon to be aForm"
RabbitIcon := aForm! !
!Rabbit methods !
birthRate
"return the value of the birth rate"
"for a rabbit default = 1 per day"
birthRate isNil ifTrue: [self birthRate: 1].
^birthRate!
dailyRange
"return the daily range of species default 500"
dailyRange isNil ifTrue: [self dailyRange: 500].
^dailyRange!
dailyRange: anInteger
"set the daily Range to be anInteger"
dailyRange := anInteger.!
displayAt: aPoint
"display the receiver's icon at aPoint"
self icon displayAt: aPoint!
foodRequirement
"return the number of days the animal can live
without food default = 1"
foodRequirement isNil ifTrue:
[self foodRequirement: 1].
^foodRequirement!
foodRequirement: anInteger
"set the number of days the animal can live
without food "
foodRequirement := anInteger!
icon
"return the RabbitIcon"
^RabbitIcon!
lastEaten: aValue
"Set the value of lastEaten."
lastEaten := aValue!
lifeExpectancy
"return the value of life expectancy"
"for a rabbit default = 10 days"
lifeExpectancy isNil
ifTrue: [self lifeExpectancy: 10].
^lifeExpectancy! !
!Grass class methods !
example
"display Icon at location"
" Grass example "
|grass|
grass := Grass new.
self icon displayAt: 100@100.!
example1
"create instance of grass default displayState false
last eaten -3 age 0 and let it grow making
displayState = true"
" Grass example1 "
| gras |
gras := Grass new grow inspect!
icon
"return the GrassIcon"
GrassIcon isNil ifTrue: [GrassIcon := Form width: 16 height: 16
bitmap: #(253 255 237 239 237 239 237 238 238 238 238 238 110 221 110 217 110 219 118 215 182 214 214 182 214 180 192 3 128 1 0 0)].
^GrassIcon!
icon: aForm
"set the icon to be aForm"
GrassIcon := aForm! !
!Grass methods !
displayAt: aPoint
"display the receiver's icon at aPoint"
self icon displayAt: aPoint!
grow
"if displayState is false and it was last
eaten > 2 days ago, set displayState to true"
( self displayState not) &
(( self age)-(self lastEaten) > 2) ifTrue:
[self displayState: true]!
icon
"return the GrassIcon"
^GrassIcon!
lastEaten
"return the number of days since grass was
last eaten default = -3"
lastEaten isNil ifTrue: [self lastEaten: -3].
^lastEaten!
lastEaten: anInteger
"set the date grass was last eaten to anInteger"
lastEaten := anInteger! !
!Fox class methods !
example
"display Icon at location"
" Fox example "
|fox|
fox := Fox new.
self icon displayAt: 100@100.!
icon
"return the FoxIcon"
FoxIcon isNil ifTrue: [FoxIcon := Form width: 16 height: 16
bitmap: #(127 255 63 255 31 255 143 255 199 255 193 255 192 31 129 129 0 0 0 42 0 42 0 0 0 15 1 255 7 255 63 255 )].
^FoxIcon!
icon: aForm
"set the icon to be aForm"
FoxIcon := aForm! !
!Fox methods !
birthRate
"return the value of the birth rate"
"for a fox default = 1 per 5 days"
birthRate isNil ifTrue: [self birthRate: 5].
^birthRate!
dailyRange
"return the daily range of species default 2000"
dailyRange isNil ifTrue: [self dailyRange: 2000].
^dailyRange!
dailyRange: anInteger
"set the daily Range to be anInteger"
dailyRange := anInteger.!
displayAt: aPoint
"display the receiver's icon at aPoint"
self icon displayAt: aPoint!
foodRequirement
"return the number of days the animal can live
without food default = 2"
foodRequirement isNil ifTrue:
[self foodRequirement: 2].
^foodRequirement!
foodRequirement: anInteger
"set the number of days the animal can live
without food "
foodRequirement := anInteger!
icon
"return the FoxIcon"
^FoxIcon!
lifeExpectancy
"return the value of life expectancy"
"for a fox default = 20 days"
lifeExpectancy isNil
ifTrue: [self lifeExpectancy: 20].
^lifeExpectancy! !
!Meadow class methods !
example
"create a meadow simulation for several days"
" Meadow example "
| days meadow |
meadow := Meadow initialize.
days := Prompter prompt: 'Run for how many days?' defaultExpression: '10'.
meadow runFor: days.!
initialize
"set up the meadow with 100 units of grass,
25 rabbits and a fox"
|rand meadow grass rabbit fox|
rand := Random new.
meadow := Meadow new.
grass := Prompter prompt: 'How many patches of grass?' defaultExpression: '100'.
rabbit := Prompter prompt: 'How many rabbits?' defaultExpression: '20'.
fox := Prompter prompt: 'How many foxes?' defaultExpression: '2'.
grass timesRepeat:[meadow grasses add: (Grass new
location: (rand next:720)@(rand next: 340))].
rabbit timesRepeat:[meadow rabbits add: (Rabbit new
location: (rand next:720)@(rand next:340);
displayState: true; age: (rand next:5))].
fox timesRepeat:[meadow foxes add: (Fox new
location: (rand next:720)@(rand next:340);
displayState: true; age:(rand next: 10))].
^meadow.! !
!Meadow methods !
date
"return the value of the current date
default is 0"
date isNil ifTrue: [self date: 0].
^date!
date: anInteger
"set the value of the current date to be anInteger"
date := anInteger!
display
"clear screen & display all inhabitants"
|field|
field := Form width: Display width height: (Display height - 10).
field displayAt: 0@10 rule: Form over.
'Meadow simulation date:', date printString displayAt: 0@0.
'Number of foxes:', foxes size printString displayAt: 200@0.
'Number of rabbits:', rabbits size printString displayAt: 350@0.
self foxes do:[:each | (each displayState) ifTrue: [each displayAt: each location]].
self rabbits do:[:each | (each displayState) ifTrue: [each displayAt: each location]].
self grasses do:[:each | (each displayState) ifTrue: [each displayAt: each location]].!
foxes
"return the bag containing all foxes"
foxes isNil ifTrue: [self foxes: Bag new].
^foxes!
foxes: aBag
"set the bag containing all foxes"
foxes := aBag!
grasses
"return the bag containing all grasses"
grasses isNil ifTrue: [self grasses: Bag new].
^grasses!
grasses: aBag
"set the bag containing all grasses"
grasses := aBag.!
morning
"add 1 to date and to everyone's age"
self date: self date + 1.
self foxes do: [:each | each age: each age+1].
self rabbits do:[:each | each age: each age+1].
self grasses do:[:each | each age: each age+1].!
rabbits
"return the bag containing all rabbits"
rabbits isNil ifTrue: [self rabbits: Bag new].
^rabbits!
rabbits: aBag
"set the bag containing all rabbits"
rabbits := aBag!
runFor: nSteps
"run simulation for integer nSteps"
nSteps timesRepeat:[self runOneStep]!
runOneStep |rand|
rand := Random new.
"run simulation one step"
"add 1 to everyone's age"
self morning.
"let grass grow"
self grasses do:[:grass | grass grow].
"let rabbits locate grass, move to it."
self rabbits do:[:rabbit | rabbit moveToPrey: (rabbit locatePrey: grasses)].
"let foxes locate rabbits, move towards them"
self foxes do:[:fox | fox moveToPrey: (fox locatePrey: rabbits)].
"let rabbits die"
self rabbits do:[:rabbit | (rabbit death)
ifTrue:[rabbits remove: rabbit ifAbsent: nil ]].
"let foxes die"
self foxes do:[:fox | (fox death)
ifTrue:[foxes remove: fox ifAbsent: nil ]].
"let rabbits give birth."
self rabbits do:[:rabbit | (rabbit birth)
ifTrue:[rabbit lastBirth: rabbit age.
rabbits add:(Rabbit new location: rabbit location)]].
"let foxes give birth"
self foxes do:[:fox | (fox birth)
ifTrue:[ fox lastBirth: fox age.
foxes add:(Fox new location: fox location)]].
"display final state"
self display! !
!MeadowInhabitant class methods !
example1
"create and display various MeadowInhabitants"
" MeadowInhabitant example1 "
|rand meadow |
rand := Random new.
meadow := Meadow new.
meadow grasses.
meadow rabbits.
meadow foxes.
25 timesRepeat:[meadow grasses add: (Grass new displayAt:(rand next:720)@(rand next:340))].
5 timesRepeat:[meadow rabbits add: (Rabbit new displayAt:(rand next:720)@(rand next:340))].
meadow foxes add: (Fox new displayAt:(rand next:720)@(rand next:340)).! !
!MeadowInhabitant methods !
age
"return the value of age default = 0"
age isNil ifTrue: [self age: 0].
^age!
age: anInteger
"set the value of age to anInteger "
age := anInteger!
displayState
"return the value of displayState default true"
displayState isNil ifTrue:[self displayState: true].
^displayState!
displayState: aBoolean
"set the value of displayState to aBoolean"
displayState := aBoolean!
location
"return the value of location default 0@0"
location isNil ifTrue: [self location: 0@0].
^location!
location: aPoint
"set the value of location to aPoint"
location := aPoint min: (720@340).! !
"construct application"
((Smalltalk at: #Application ifAbsent: [])
isKindOf: Class) ifTrue: [
((Smalltalk at: #Application) for:'MeadowSimulation')
addClass: MeadowAnimal;
addClass: Rabbit;
addClass: Grass;
addClass: Fox;
addClass: Meadow;
addClass: MeadowInhabitant;
comments: nil;
initCode: nil;
finalizeCode: nil;
startUpCode: nil]!